<?php 
if (!$_SESSION['order']['order_id']) redirect(HTML_BASE);



if ($_POST['is_submitted']) {
	
	$doPay = new DoExpressCheckoutPayment("".$_SESSION['order']['price']."");
	$doPay->setNVP("TOKEN", urlencode($_POST['token'])); // ???
    $doPay->setNVP("PAYERID", urlencode($_POST['PayerID'])); // ???
	$result = $doPay->getResponse(); 
	
} else {
	
	$response = GetExpressCheckoutDetails::request(); 
}






$sql = "SELECT
			 
			  d.title,
			  d.meta_description,
			  d.description,
			  d.meta_key
		  FROM
			  pages AS c,
			  page_description AS d
		  WHERE
			c.page_id = d.page_id
			AND
			d.lang_id = '".$_SESSION['lang_id']."'
			AND
			c.sef_url = 'order-confirm' 
			
		  ";
$res = query($sql);
$row = mysql_fetch_object($res);


head(sth($row->title),sth($row->meta_description),sth($row->meta_key));


//echo $_SESSION['first_name'].$_SESSION['last_name'];

//echo $_SESSION['country'].$_SESSION['country-name'];
?>
<div id="right">
			<div class="header">
				<h1><?=__New_Semiotics?></h1>
				<h2><?=__Between_Tradition?></h2>
			</div>
			
			<div id="content">
			<h3><?=stripslashes($row->title)?></h3> 
				
				<?=stripslashes($row->description)?>	
				<?php 
	
	if (!$_POST['is_submitted'] && $response['ACK'] == 'Success' && $response['EMAIL']) {
		
		$info = $response['FIRSTNAME'].' '.$response['LASTNAME'].' , '.$response['EMAIL'];
		# Update the order status to Paid
		$sql = "UPDATE orders
				SET
					paypal = '".$info."'
				WHERE
					order_id = '".$_SESSION['order']['order_id']."'";
		query($sql);
	?>
    	
        <h5 style="color:red;"><?=__successfully_authorized?></h5>
        <br>
        
        
        <form id="send-form" action="<?=HTML_BASE.$_SESSION['lang']?>/order-confirm" method="post" enctype="multipart/form-data">
        <input type="hidden" name="is_submitted" value="1" />
        <input type="hidden" name="token" value="<?=$_GET['token']?>" />
        <input type="hidden" name="PayerID" value="<?=$_GET['PayerID']?>" />
                
       
        <div>
            
            <b><?=__Order_information?></b>
            12th world Congress of Semiotics Sofia 2014, 16-20 September. New Bulgarian University(Registration #<?=number_pad($_SESSION['order']['order_id'],5)?>);
            
            <?php
            
			$price = $_SESSION['order']['price'];
			$add = '';
			if($_SESSION['order']['iass']){
				//$price -= 25;
				$add .= __MESSAGE_IASS;
				
				if($_SESSION['country'] == 3 && $_SESSION['country-name'] != 'ARGENTINA' && $_SESSION['country-name'] != 'VENEZUELA, BOLIVARIAN REPUBLIC OF')
				{
					$price -= 15;
				}else{
					$price -= 25;
				}
			}
			
			if($_SESSION['order']['culinary']){
				$price -= 35;
				$add .= __MESSAGE_Culinary;
			}
			
			if($_SESSION['order']['dinner']){
				$price -= 35;
				$add .= __MESSAGE_dinner;
			}
			
			
		# Mail to User
		
		$add = sprintf(__MESSAGE_TAX,$price).$add; 
		
		
		
		echo $add;

			
			?>
            
            
            
            <div class="padding">
            	<br>
                
                <div id="price">
					<?=__Participation?><br>
					<div>&euro; <span id="priceE"><?=$_SESSION['order']['price']?></span></div>
					($ <span id="priceD"><?=$_SESSION['order']['priceD']?></span>)<br><br>
					
					<a class="button" onclick="$('#send-form').submit();"  ><?=__Pay_now?></a>
				</div>
                
                
				
				<br><br>
                
                
            </div>
          
        
        </div>        
        
        </form>
          
    <?php
	} elseif (!$_POST['is_submitted'] && !$response['EMAIL']) {
		
		//unset($_SESSION['order']);
		
		
	?>
    	
        <h5 style="color:red;"><?=__Authorization_not?></h3>
        <br>
        <?=sprintf(__Authorization_not_text,HTML_BASE.$_SESSION['lang'].'/registration-form/'.base64_encode($_SESSION['order']['order_id']),number_pad($_SESSION['order']['order_id'],5))?>
        
        
    <?php
	$pay_url = HTML_BASE.$_SESSION['lang'].'/registration-form/'.base64_encode($_SESSION['order']['order_id']);
		
	$oredera = number_pad($_SESSION['order']['order_id'],5);
		
	    mail( $_SESSION['USER_EMAIL'], 
	          USER_ORDER_ERROR_SUBJECT,
	          sprintf(USER_ORDER_ERROR_MESSAGE,
			  $_SESSION['first_name'], $_SESSION['last_name'],
			  $pay_url,$pay_url,$oredera),
	          HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
		
	
	
	
	
	} elseif ( $result['ACK'] == 'Success') {
				
		# Update the order status to Paid
		$sql = 'UPDATE orders
				SET
					status = 2
				WHERE
					order_id = '.$_SESSION['order']['order_id'].'
				';
		query($sql);
		
		$price = $_SESSION['order']['price'];
		$add = '';
		if($_SESSION['order']['iass']){
			//$price -= 25;
			$add .= __MESSAGE_IASS;
			
			if($_SESSION['country'] == 3 && $_SESSION['country-name'] != 'ARGENTINA' && $_SESSION['country-name'] != 'VENEZUELA, BOLIVARIAN REPUBLIC OF')
				{
					$price -= 15;
				}else{
					$price -= 25;
				}
 		}
		
		if($_SESSION['order']['culinary']){
			$price -= 35;
			$add .= __MESSAGE_Culinary;
 		}
		
		if($_SESSION['order']['dinner']){
			$price -= 35;
			$add .= __MESSAGE_dinner;
 		}
		
		
		# Mail to User
		$oredera = number_pad($_SESSION['order']['order_id'],5);
	    mail( $_SESSION['USER_EMAIL'], 
	          sprintf(USER_ORDER_CONFIRM_SUBJECT, $oredera ),
	          sprintf(USER_ORDER_CONFIRM_MESSAGE,
			  $_SESSION['first_name'], $_SESSION['last_name'],
			  $oredera,$price, $add), 
	          HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
			  
		mail( ADMINISTRATOR_EMAIL, 
	          sprintf(USER_ORDER_CONFIRM_SUBJECT, $oredera ),
	          sprintf(USER_ORDER_CONFIRM_MESSAGE,
			  $_SESSION['first_name'], $_SESSION['last_name'],
			  $oredera,$price, $add), 
	          HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");  
	          
	    
		
	?>
		        
        <h2><?=__Thank_you?></h2>
        <br>
        <?=__Thank_you_text?>
        
        <br><br />

        
        <a class="button" href="<?=HTML_BASE.$_SESSION['lang']?>"><?=__Continue?></a>
		
                    
	<?php
		
		//unset($_SESSION['order']);
		
	} elseif ( $result['ACK'] == 'Failure') {
		
		
		
		# Update the order status to Failed
		$sql = 'UPDATE orders
				SET
					status = 4
				WHERE
					order_id = '.$_SESSION['order']['order_id'].'
				';
		query($sql);
		
		# Mail to User ERROR
		$pay_url = HTML_BASE.$_SESSION['lang'].'/registration-form/'.base64_encode($_SESSION['order']['order_id']);
		
	$oredera = number_pad($_SESSION['order']['order_id'],5);
		
	    mail( $_SESSION['USER_EMAIL'], 
	          USER_ORDER_ERROR_SUBJECT,
	          sprintf(USER_ORDER_ERROR_MESSAGE,
			  $_SESSION['first_name'], $_SESSION['last_name'],
			  $pay_url,$pay_url,$oredera),
	          HTML_HEADERS."From: Semio 2014 <".NO_REPLY_EMAIL.">");
		
		
		//unset($_SESSION['order']);
	?> 
    	
    	<h5 style="color:red;"><?=__Authorization_not?></h5>
        <br>
        <?=sprintf(__Authorization_not_text,HTML_BASE.$_SESSION['lang'].'/registration-form/'.base64_encode($_SESSION['order']['order_id']),number_pad($_SESSION['order']['order_id'],5))?>

      
    	
    <?php 
	}
	?>
    
	</div>
<?php

foot();
?>